﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using log4net;
using System.Diagnostics;
using System.Reflection;
using Safover.LidarCollector.Core;

namespace Om.Default
{
    class NjOpticalMaserPacketFactory
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static string Name => "NjOpticalMaserPacketFactory";

        public static NJOpticalMaserPacket BuildPacket(byte[] rawBytes)
        {
            return Build(rawBytes);
        }

        private static NJOpticalMaserPacket Build(byte[] rawBytes)
        {
            Debug.Assert(rawBytes != null);

            if (rawBytes.Length != NJOpticalMaserDevice.FIXED_PACKET_LENGTH)
            {
                throw new PacketProtocolException($"{Name}: Illegal packet protocol format, data=[{SerialPortDevice.ToHexString(rawBytes)}].");
            }

            if (rawBytes[2] == 0x00)
            {
                return new MainBoardPacket(rawBytes);
            }
            else if (rawBytes[2] == 0x0A)
            {
                return new DriverBoardPacket(rawBytes);
            }
            else if (rawBytes[2] == 0x3C)
            {
                return new LDTemperatureBoardPacket(rawBytes);
            }
            else if (rawBytes[2] == 0x3E)
            {
                return new LaserCrystalTemperatureBoardPacket(rawBytes);
            }
            else if (rawBytes[2] == 0x3F)
            {
                return new FrequencyDoublingCrystalTemperatureBoardPacket(rawBytes);
            }
            else if (rawBytes[2] == 0x3D)
            {
                return new DeprecatedPacket(rawBytes);
            }
            else
            {
                Log.Error($"{Name}: Undefined packet received: [{SerialPortDevice.ToHexString(rawBytes)}]");
                return null;
            }
        }
    }

}
